package com.module.imagelib.glide;

import android.content.Context;
import android.net.Uri;

import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.module.imagelib.glide.strategy.ILoaderStrategy;
import com.module.imagelib.glide.utils.ImageFileUtils;

import java.io.File;

/**
 * 2.0版
 */
public class ImageLoader {

    private static ImageLoader instance;
    private final static Object syncLock = new Object();
    private static ILoaderStrategy sLoader;

    private ImageLoader() {

    }

    public static ImageLoader getInstance() {
        if (instance == null) {
            synchronized (syncLock) {
                if (instance == null) {
                    instance = new ImageLoader();
                }
            }
        }
        return instance;
    }

    public LoaderOptions load(String path) {
        return new LoaderOptions(path);
    }

    public LoaderOptions load(int drawable) {
        return new LoaderOptions(drawable);
    }

    public LoaderOptions load(File file) {
        return new LoaderOptions(file);
    }

    public LoaderOptions load(Uri uri) {
        return new LoaderOptions(uri);
    }

    //提供实时替换图片加载框架的接口
    public void setImageLoader(ILoaderStrategy loader) {
        if (loader != null) {
            sLoader = loader;
        }
    }

    public void loadOptions(LoaderOptions options) {
        sLoader.loadImage(options);
    }

    public void clearMemoryCache() {
        sLoader.clearMemoryCache();
    }

    public void clearDiskCache() {
        sLoader.clearDiskCache();
    }

    public void pauseRequests() {
        sLoader.pauseRequests();
    }

    public void resumeRequests() {
        sLoader.resumeRequests();
    }

    /**
     * 获取缓存大小
     */
    public String getCacheSize(Context context) {
        try {
            return ImageFileUtils.getFormatSize(new File(context.getApplicationContext().getCacheDir() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}

